// pages/shareHelp/shareHelp.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageUrl: 'https://armeco.happydoit.com/upload', // 服务器图片路径
    sureHelp: false, // 助力弹窗
    hasHelp: false, // 是否已经助力过
    cityId: -1, // 助力城市id
    userid: -1, // 好友id
    cityName: '', // 助力城市名称
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      cityName: options.cityName,
      cityId: options.cityId,
      userid: options.userid,
      canFocus: app.globalData.canFocus
    })
    if (app.globalData.jessionId) {
      if (app.globalData.userInfo.id == options.userid) {
        wx.redirectTo({
          url: '/pages/stage/myHelpHistory/myHelpHistory?cityName=' + options.cityName + '&&cityId=' + options.cityId,
        })
      } 
    } else {
      app.wxLogin().then(function () {
        if (app.globalData.userInfo.id == options.userid) {
          wx.redirectTo({
            url: '/pages/stage/myHelpHistory/myHelpHistory?cityName=' + options.cityName + '&&cityId=' + options.cityId,
          })
        } 
      }, function () {
        console.log("登录失败")
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this;
    return {
      title: '让邓伦来我的芯愿城市，就靠你的助力了！',
      path: '/pages/stage/cityHelp/cityHelp',
      imageUrl: that.data.imageUrl + '/cityimages/share-img.png'
    }
  },

  // 助力
  toHelp: function () {
    let that = this;
    wx.showLoading({
      title: '加载中',
      mask: true,
    });
    let data = {
      cityid: that.data.cityId,
      frienduserid: that.data.userid
    }
    console.log(that.data.cityId)
    console.log(that.data.userid)
    wx.request({
      url: app.globalData.requestUrl + '/api/city/shareCityAss',
      data: data,
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Cookie": "JSESSIONID=" + app.globalData.jessionId
      },
      success: function (res) {
        if (res.data.code == 'success') {
          wx.hideLoading();
          that.setData({
            sureHelp: true
          })
        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            mask: true
          })
        }
      }
    })
  },

  // 关闭助力弹窗
  closeHelp: function () {
    this.setData({
      sureHelp: false
    })
  },

  joinPlay: function () {
    wx.redirectTo({
      url: '/pages/heloo/index/index',
    });
  },

  showFocus() {
    this.setData({
      isFocus: true
    })
  },

  hideFocus() {
    this.setData({
      isFocus: false
    })
  },

  openRule() {
    this.setData({
      showRule: true
    })
  },

  onGetCode: function (e) {
    this.setData({
      showRule: e.detail.val
    })
  }
})